package com.zhou.bjgl.bjgl.business_web.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhou.bjgl.bjgl.business_web.entity.BCourse;
import com.zhou.bjgl.bjgl.exception.BwException;
import com.zhou.bjgl.bjgl.exception.BwExceptionEnum;
import com.zhou.bjgl.bjgl.business_web.mapper.BClassMapper;
import com.zhou.bjgl.bjgl.business_web.mapper.BCourseMapper;
import com.zhou.bjgl.bjgl.req.b_course.BCourseQueryReq;
import com.zhou.bjgl.bjgl.req.b_course.BCourseAddReq;
import com.zhou.bjgl.bjgl.resp.BCourseResp;
import com.zhou.bjgl.bjgl.resp.PageResp;
import com.zhou.bjgl.bjgl.business_web.service.BCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.bjgl.bjgl.util.CopyUtil;
import com.zhou.bjgl.bjgl.util.coder.IdGeneratorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  课堂servcie实现类
 * </p>
 *
 * @author jz
 * @since 2021-07-04
 */
@Service
public class BCourseServiceImpl extends ServiceImpl<BCourseMapper, BCourse> implements BCourseService {

    @Resource
    private BCourseMapper bCourseMapper;

    @Autowired
    private BClassMapper bClassMapper;

    /**
     * 添加班级方法
     * @param req
     * @return
     */
    @Override
    public String add(BCourseAddReq req) {
        BCourse course = CopyUtil.copy(req, BCourse.class);

        course.setClassId(req.getClassId());

        //生成课堂识别码
        String code = IdGeneratorUtil.makeCode("nj");
        course.setCourseCode(code);
        course.setStatus(0);
        int insert = bCourseMapper.insert(course);
        if (insert == 0) {
            throw new BwException(BwExceptionEnum.INSERT_FAIL);
        }
        return code;
    }

    /**
     * 获取签到课程列表
     * @param req
     * @return
     */
    @Override
    public PageResp<BCourseResp> getCourseList(BCourseQueryReq req) {

        PageHelper.startPage(req.getPage(),req.getSize());
        List<BCourseResp> courseList=bCourseMapper.queryCourseList(req);
        PageResp<BCourseResp> bCourseRespPageResp = new PageResp<>();

        PageInfo<BCourseResp> bCourseRespPageInfo = new PageInfo<>(courseList);

        bCourseRespPageResp.setTotal(bCourseRespPageInfo.getTotal());
        bCourseRespPageResp.setPageList(courseList);

        return bCourseRespPageResp;
    }

    @Override
    public void delete(BCourseQueryReq bCourseQueryReq) {
        bCourseMapper.deleteById(bCourseQueryReq.getId());
    }
}
